﻿namespace LookupWithPicker
{
    using System;
    using System.Collections.Generic;
    using System.Text;
    using Microsoft.SharePoint.WebControls;
    using System.Data;
    using Microsoft.SharePoint;

    public class CustomLookupQueryControl : SimpleQueryControl
    {
        public static string NAME_ID = "ID";
        public static string NAME_TITLE = "Title";

        public CustomLookupQueryControl()
        {
            Load += this.CustomLookupQueryControl_Load;
        }

        public override PickerEntity GetEntity(DataRow dr)
        {
            PickerEntity entity = new PickerEntity();
            entity.DisplayText = "" + dr[NAME_TITLE];
            entity.Key = "" + dr[NAME_ID];
            entity.Description = "" + dr[NAME_TITLE];
            entity.IsResolved = true;
            return entity;
        }

        protected void CustomLookupQueryControl_Load(object sender, EventArgs e)
        {
            using (SPWeb web = SPContext.Current.Site.OpenWeb(this.GetWebId()))
            {
                SPList list = web.Lists[this.GetListId()];
                string fieldName = this.GetFieldName();
                CustomLookupQueryControl.NAME_ID = list.Fields.GetFieldByInternalName("ID").Title;
                CustomLookupQueryControl.NAME_TITLE = list.Fields.GetFieldByInternalName(fieldName).Title;

                if (!Page.IsPostBack)
                {
                    EnsureChildControls();
                    mColumnList.Items.Add(NAME_TITLE);
                    mColumnList.Items.Add(NAME_ID);
                }
            }
        }

        protected override int IssueQuery(string search, string groupName, int pageIndex, int pageSize)
        {
            DataTable dummyTable = this.GetListTable(search, groupName);

            PickerDialog.Results = dummyTable;
            PickerDialog.ResultControl.PageSize = dummyTable.Rows.Count;

            return dummyTable.Rows.Count;
        }

        private DataTable GetListTable(string search, string groupName)
        {
            using (SPWeb web = SPContext.Current.Site.OpenWeb(this.GetWebId()))
            {
                SPList list = web.Lists[this.GetListId()];
                string fieldName = this.GetFieldName();

                DataTable dummyTable = new DataTable();
                dummyTable.Columns.Add(NAME_ID);
                dummyTable.Columns.Add(NAME_TITLE);

                //SPWeb web = SPControl.GetContextWeb(Context);
                //SPList list = web.Lists[new Guid(((CustomLookupPickerDialog)this.PickerDialog).CustomProperty)];
                SPQuery query = new SPQuery();
                if (!string.IsNullOrEmpty(search))
                {
                    if (groupName.Equals(NAME_ID))
                    {
                        query.Query = string.Format("<Where><Eq><FieldRef Name=\"ID\"/><Value Type=\"Number\">{0}</Value></Eq></Where>", search);
                    }
                    else if (groupName.Equals(NAME_TITLE))
                    {
                        query.Query = string.Format("<Where><Contains><FieldRef Name=\"{0}\"/><Value Type=\"Text\">{1}</Value></Contains></Where>", fieldName, search);
                    }
                }

                SPListItemCollection items = list.GetItems(query);
                foreach (SPListItem item in items)
                {
                    DataRow row = dummyTable.NewRow();
                    row[NAME_ID] = item["ID"];
                    row[NAME_TITLE] = SPFieldLookupWithPicker.GetFieldValue(item, this.GetFieldName());
                    dummyTable.Rows.Add(row);
                }

                return dummyTable;
            }
        }

        private Guid GetListId()
        {
            string listId = ((CustomLookupPickerDialog)this.PickerDialog).CustomProperty.Split(new char[] { '#' })[0];
            return new Guid(listId);
        }

        private string GetFieldName()
        {
            string fieldName = ((CustomLookupPickerDialog)this.PickerDialog).CustomProperty.Split(new char[] { '#' })[1];
            return fieldName;
        }

        private Guid GetWebId()
        {
            string webId = ((CustomLookupPickerDialog)this.PickerDialog).CustomProperty.Split(new char[] { '#' })[2];
            return new Guid(webId);
        }
    }
}
